import kotlinx.browser.document
import openfl.display.BitmapData
import openfl.display.Stage
import starling.core.Starling
import starling.display.Image
import starling.display.Sprite
import starling.display.Sprite3D
import starling.events.EnterFrameEvent
import starling.textures.Texture
import kotlin.math.sin

@JsExport
class Game : Sprite() {

    init {
        BitmapData.loadFromFile("photo.jpg").onComplete {
            val t = Texture.fromBitmapData(it)
            val img = Image(t)
            img.width = 200.0
            img.height = 200.0

            val s = Sprite3D()
            s.addChild(img)

            s.pivotX = img.width / 2
            s.pivotY = img.height / 2
            s.x = (stage.stageWidth / 2).toDouble()
            s.y = (stage.stageHeight / 2).toDouble()
            addChild(s)

            var step = 0.0
            addEventListener(EnterFrameEvent.ENTER_FRAME) {
                step += 0.1
                s.rotationY = sin(step) / 2
            }
        }
    }
}

fun main() {
    val stage = Stage(550, 400, 0x00000000)
    Starling(Game::class.js, stage).start()
    document.body?.appendChild(stage.element)
}